package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.UserStudyPlan;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserStudyPlanMapper {
    
    @Select("SELECT * FROM user_study_plans WHERE user_id = #{userId}")
    List<UserStudyPlan> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM user_study_plans WHERE plan_id = #{planId}")
    UserStudyPlan findById(@Param("planId") Long planId);
    
    @Select("SELECT p.* FROM user_study_plans p " +
            "INNER JOIN user_learning_progress l ON p.user_id = l.user_id " +
            "WHERE p.user_id = #{userId} AND l.course_id = #{courseId}")
    List<UserStudyPlan> findByUserIdAndCourseId(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    @Insert("INSERT INTO user_study_plans (user_id, plan_name, start_date, end_date, " +
            "daily_target, current_progress, status, create_time) " +
            "VALUES (#{userId}, #{planName}, #{startDate}, #{endDate}, " +
            "#{dailyTarget}, #{currentProgress}, #{status}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "planId")
    int insert(UserStudyPlan plan);
    
    @Update("UPDATE user_study_plans SET plan_name = #{planName}, " +
            "start_date = #{startDate}, end_date = #{endDate}, " +
            "daily_target = #{dailyTarget}, current_progress = #{currentProgress}, " +
            "status = #{status}, update_time = NOW() " +
            "WHERE plan_id = #{planId}")
    int update(UserStudyPlan plan);
    
    @Update("UPDATE user_study_plans SET current_progress = #{progress}, " +
            "status = #{status}, update_time = NOW() " +
            "WHERE plan_id = #{planId}")
    int updateProgress(@Param("planId") Long planId, 
                      @Param("progress") Integer progress, 
                      @Param("status") Integer status);
    
    @Delete("DELETE FROM user_study_plans WHERE plan_id = #{planId}")
    int deleteById(@Param("planId") Long planId);
} 